package com.itheima.service.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.QuoteException;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    //分页查询检查组信息
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //@Transactional
        Page<CheckGroup> page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()).doSelectPage(
                () -> checkGroupDao.findPage(queryPageBean.getQueryString()));
        return new PageResult(page.getTotal(), page.getResult());
    }

    //添加检查组数据
    @Override
    public Result add(CheckGroup checkGroup, Integer[] checkitemIds) {

        try {
            if (checkitemIds.length == 0 || checkitemIds == null) {
                throw new QuoteException();
            }
            checkGroupDao.add(checkGroup);
            //设置检查组合和检查项的关联关系
           setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (QuoteException e) {
            return new Result(false, "检查项不能为空,至少需要选择一个");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);

    }
    //设置检查组合和检查项的关联关系
    private void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        Map<String, Object> map = new HashMap<>();
        map.put("checkGroupId", checkGroupId);
        map.put("checkItemIds", checkitemIds);
        checkGroupDao.setCheckGroupAndCheckItem(map);
    }


    //删除检查组信息
    @Override
    public Result deleteById(Integer id) {
        //查询关系表  组和套餐d  项和组关系表
        //t_setmeal_checkgroup   t_checkgroup_checkitem
        try {
            //组和套餐d
            long count = checkGroupDao.findCountByCheckGroupId(id);
            if (count > 0) {
                throw new QuoteException("该检查组已经被引用,不能删除");
            }
            //先解除检查组和检查项的关系
            checkGroupDao.deleteCheckgroupCheckitem(id);
            //执行删除组
            checkGroupDao.deleteById(id);
        } catch (QuoteException e) {
            return new Result(false, "该检查组已经被引用,不能删除");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);

    }


    //根据id查询该检查组的具体数据
    @Override
    public Result findById(Integer id) {

        try {
            CheckGroup checkGroup = checkGroupDao.findById(id);
            if (checkGroup != null) {
                return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }


    //根据检查组的id查询关联表中检查项id
    @Override
    public Result findCheckItemIdsByCheckGroupId(Integer id) {

        try {
            List<Integer> checkItemIds = checkGroupDao.findCheckItemIdsByCheckGroupId(id);
            return new Result(true, "回显检查项id成功", checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    //编辑修改检查组的数据
    @Override
    public Result edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            //判断检查项是否为空
            if (checkitemIds == null || checkitemIds.length == 0) {
                return new Result(false, "至少要选择一个检查项");
            }
            //更新检查组信息
            checkGroupDao.edit(checkGroup);
            //解除项组关系表
            checkGroupDao.deleteCheckgroupCheckitem(checkGroup.getId());
            //设置检查组合和检查项的关联关系
           setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
    }
    //查询所有检查组
    @Override
    public Result findAll() {
        try {
            List<CheckGroup>checkGroupList= checkGroupDao.findAll();
            if(CollectionUtils.isEmpty(checkGroupList)){
                return  new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
            }
            return  new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
        }return  new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }
}
